<?php
session_start();  

function login(){
	include('../Model/db.php');
	include('../Model/UserAdmin.php');
	$useradmin = new Useradmin();
	if(isset($_SESSION['wkadmin'])){
		$useradmin->username = $_SESSION['wkadmin'];
		$useradmin->name = $_SESSION['wkadminname'];
	}
	else{
		connectDB();
		$username = $_POST['username'];
		$password = sha1($_POST['password']);
		$useradmin->getUser($username,$password);
		if($useradmin->username != ''){
			$_SESSION['wkadmin'] = $useradmin->username;
			$_SESSION['wkadminname'] = $useradmin->name;
		}
		disconnectDB();
	}
	echo json_encode($useradmin);
}

function checkLogin(){
	include('../Model/UserAdmin.php');
	$useradmin = new Useradmin();
	if(isset($_SESSION['wkadmin'])){
		$useradmin->username = $_SESSION['wkadmin'];
		$useradmin->name = $_SESSION['wkadminname'];
	}
	echo json_encode($useradmin);
}

function logout(){
	session_destroy();
}

if($_POST){
	if(isset($_POST['action'])){
	
		$action = $_POST['action'];
		switch($action){
			case 'login' : login(); break;
			case 'checkLogin' : checkLogin(); break;
			case 'logout' : logout(); break;
			default: break;
		}
	}
}

if($_GET){
	if(isset($_GET['action'])){
		if($_GET['action']=='logout'){
			session_destroy();
		}
	}
}
?>